package com.example.oppotest.controller;

import com.alibaba.fastjson.JSON;
import com.example.oppotest.entity.youku.AdInfo;
import com.example.oppotest.entity.youku.Effpoint;
import com.example.oppotest.entity.youku.youkuCount;
import com.example.oppotest.service.EffpointService;
import com.example.oppotest.util.NetUtils;
import com.example.oppotest.util.youkuUtils;
import org.apache.commons.collections4.map.ListOrderedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author wyk
 * @Date: 2025/2/26 21:47
 */

@RestController
@RequestMapping("/youku")
public class YoukuController extends BaseController{

    @Autowired
    private EffpointService effpointService;

    @Autowired
    private RestTemplate restTemplate;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @RequestMapping(value = "/selectyoukuCountAll", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap selectyoukuCountAll(HttpServletRequest request) {
        ModelMap map = new ModelMap();
        List<youkuCount> list = effpointService.selectyoukuCountAll();
        map.put("data", list);
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("count", list.size());
        return map;
    }

    @RequestMapping(value = "/selectEffpoint", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap selectEffpoint(HttpServletRequest request) {
        ModelMap map = new ModelMap();
        List<Effpoint> list = effpointService.selectEffpoint();
        map.put("data", list);
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("count", list.size());
        return map;
    }

    @RequestMapping(value = "/selectAdInfo", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap selectAdInfo(HttpServletRequest request) {
        ModelMap map = new ModelMap();
        List<AdInfo> list = effpointService.selectAdInfo();
        map.put("data", list);
        map.put("code", 200);
        map.put("msg", "成功");
        map.put("count", list.size());
        return map;
    }

    /**
     * 后台创建有效触点监控地址
     *
     * @param effpoint 传入的有效点对象
     * @param request  HTTP 请求对象
     * @return 返回处理结果的模型映射对象
     * @throws Exception 可能抛出异常
     */
    @RequestMapping(value = "/createEffpoint", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap createEffpoint(@RequestBody Effpoint effpoint, HttpServletRequest request){
        ModelMap map = new ModelMap();
        if (effpoint == null) {
            sendFailureMessage(map, "未接收到原始数据");
        } else {
//            if (!StringUtils.hasText(effpoint.getCreative_id())) {
//                effpoint.setCreative_id("110001");
//            }
            map = effpointService.createEffpoint(effpoint, request);
        }
        return map;
    }

    @RequestMapping(value = "/forwardToAlipay", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap forwardToAlipay(@RequestParam(value = "url") String url) throws IOException {
        ModelMap map = new ModelMap();
        // Python 接口的 URL
        String pythonApiUrl = "https://www.xiaogao.site:442/dpapi/v1/zfb/";
        Map<String, Object> sendParams = new ListOrderedMap<>();
        sendParams.put("url", url);
        String result = NetUtils.postByRaw(pythonApiUrl, JSON.toJSONString(sendParams));
        logger.info("Alipay：" + result);
        map.put("result", result);
        sendSuccessMessage(map, "成功");
        return map;
    }

    @RequestMapping(value = "/forwardToTaobao", method = RequestMethod.POST)
    @ResponseBody
    public ModelMap forwardToTaobao(@RequestParam(value = "url") String url) throws IOException {
        ModelMap map = new ModelMap();
        // Python 接口的 URL
        String pythonApiUrl = "https://www.xiaogao.site:442/dpapi/v1/taobao/";
        MyRequest myRequest = new MyRequest();
        myRequest.setUrl(url);
        String result = restTemplate.postForObject(pythonApiUrl, myRequest, String.class);
        logger.info("Taobao：" + result);
        map.put("result", result);
        sendSuccessMessage(map, "成功");
        return map;
    }

    public static class MyRequest {
        private String url;

        public String getUrl() {
            return url;
        }

        public void setUrl(String url) {
            this.url = url;
        }
    }


//    @GetMapping("/realEffpoint/{effpointId}")
//    @ResponseBody
//    public ModelMap realEffpoint(@PathVariable Integer effpointId, HttpServletRequest request) throws IOException {
//        Effpoint effpoint = effpointService.selectEffpointLimit(effpointId);
//        if (effpoint == null) {
//            ModelMap map = new ModelMap();
//            sendFailureMessage(map, "正式联调，未接收到原始数据");
//            return map;
//        } else {
//            return effpointService.createEffpoint(effpoint, request);
//        }
//    }


}
